package cn.life.business.user.dao.impl;

import cn.life.business.notice.entity.po.NoticeUserPo;
import cn.life.business.user.entity.vo.LoginUserVo;
import cn.life.business.user.entity.vo.UserInfoVo;
import cn.life.common.dao.CommonDao;
import cn.life.common.entity.model.CMessage;
import cn.life.common.entity.po.ProjectPo;
import cn.life.common.entity.vo.Pagination;
import cn.life.common.support.jdbc.AbstractSupportDao;
import cn.life.business.user.dao.UserDao;
import cn.life.business.user.entity.po.LoginUser;
import cn.life.business.user.entity.po.UserInfo;
import cn.life.business.user.entity.vo.RegisterUserVo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author 李佳明
 * @Date 2018/7/5 0005.6:59.
 */
@Repository
public class UserDaoImpl extends AbstractSupportDao implements UserDao{

    @Resource
    private CommonDao commonDao;

    @Override
    public Long addLoginUser(RegisterUserVo loginUser) {
        Long loginUserId = commonDao.getNextId("login_user","id");
        String sql = "insert into login_user(id,user_name,login_name,login_password,opr_date) values(?,?,?,?,?)";
        return update(sql,loginUserId,loginUser.getUserName(),loginUser.getLoginName(),loginUser.getLoginPassword(),getSqlDate(new Date())) == 1  ? loginUserId : 0 ;
    }

    @Override
    public UserInfo login(LoginUser loginUser) {
        String sql = "select b.user_id,b.user_name,b.user_phone,b.user_email,b.factory_id,b.factory_name,b.jg_push_mark,b.hw_push_mark,c.user_role from login_user a inner join user_info b on a.id = b.login_user_id inner join user_role c on b.user_id = c.user_id and c.relation_state = 1 where a.login_name = ? and a.login_password = ?";
        return queryForObject(sql,UserInfo.class,loginUser.getLoginName(),loginUser.getLoginPassword());
    }

    @Override
    public int addUserInfo(UserInfo user) {
        String sql = "insert into user_info(user_name,login_user_id,user_sex,user_age,user_id_card,user_phone,user_email,opr_date,is_manager,factory_id,factory_name,jg_push_mark) values(?,?,?,?,?,?,?,?,?,?,?,?)";
        return update(sql,user.getUserName(),user.getLoginUserId(),user.getUserSex(),user.getUserAge(),user.getUserIdCard(),user.getUserPhone(),user.getUserEmail(),getSqlDate(new Date()),user.getIsManager(),user.getFactoryId(),user.getFactoryName(),user.getJgPushMark());
    }

    @Override
    public int updateUserInfo(UserInfo userInfo) {
        String sql = "update user_info set user_name = ?,user_phone = ?,jg_push_mark = ?,hw_push_mark = ? where user_id = ?";
        return update(sql,userInfo.getUserName(),userInfo.getUserPhone(),userInfo.getJgPushMark(),userInfo.getHwPushMark(),userInfo.getUserId());
    }

    public int uodateUserRole(UserInfo userInfo){
        String sql = "update user_role set user_role = ? where factory_id = ? and project_id = ?";
        return update(sql,userInfo.getUserRole(),userInfo.getFactoryId(),userInfo.getProjectId());
    }

    @Override
    public UserInfo getUserInfo(UserInfo userInfo) {
        String sql = "select * from user_info where user_id = ?";
        return queryForObject(sql,UserInfo.class,userInfo.getUserId());
    }

    @Override
    public LoginUser getLoginUserInfo(Long id) {
        String sql = "select * from login_user where id = ?";
        return queryForObject(sql,LoginUser.class,id);
    }

    @Override
    public LoginUser getLoginUserInfo(String loginName) {
        String sql = "select * from login_user where login_name = ?";
        return queryForObject(sql,LoginUser.class,loginName);
    }

    @Override
    public Pagination<UserInfo> getUserList(UserInfo userInfo) {
        String sql = "select distinct a.* from user_info a left join user_role b on a.user_id = b.user_id where a.factory_id = ?";
        if(userInfo.getUserName() != null && !userInfo.getUserName().equals("")){
            sql = sql + " and a.user_name like '%" + userInfo.getUserName() + "%'";
        }
        sql=sql+" ORDER BY opr_date DESC";
        return queryForListByPageEx(sql,UserInfo.class,userInfo.getPageIndex(),userInfo.getPageSize(),userInfo.getFactoryId());
    }

    @Override
    public UserInfoVo queryDetail(UserInfo userInfo) {
        String sql = "select a.*,b.login_name,b.login_password,ifnull(c.user_role,0) as userRole from user_info a inner join login_user b on a.login_user_id = b.id left join user_role c on a.user_id = c.user_id and c.project_id = ? where a.factory_id = ? and a.user_id = ? ";
        return queryForObject(sql,UserInfoVo.class,userInfo.getProjectId(),userInfo.getFactoryId(),userInfo.getUserId());
    }

    @Override
    public int updateUserRole(UserInfoVo userInfo) {
        //1.新增角色时，会将其他项目的状态置为已过期状态
        String firstSql = "update user_role set relation_state = 0 where user_id = ?";
        update(firstSql,userInfo.getUserId());
        String sql = "update user_role set user_name = ?,user_role = ?,relation_state = 1 where project_id = ? and user_id = ? ";
        return update(sql,userInfo.getUserName(),userInfo.getUserRole(),userInfo.getProjectId(),userInfo.getUserId());
    }

    public Long queryLoginUserId(UserInfoVo userInfoVo){
        String sql = "select id from login_user a inner join user_info b on a.id = b.login_user_id and b.user_id = ?";
        return queryForObject(sql,Long.class,userInfoVo.getUserId());
    }

    @Override
    public int updateLoginData(UserInfoVo userInfoVo) {
        Long id = queryLoginUserId(userInfoVo);
        String sql = "update login_user set login_name = ?,login_password = ? where id = ?";
        return update(sql,userInfoVo.getLoginName(),userInfoVo.getLoginPassword(),id);
    }

    @Override
    public boolean verification(RegisterUserVo user) {
        String sql = "select count(1) from login_user where login_name = ?";
        int result = queryForObject(sql,Integer.class,user.getLoginName());
        return result > 0 ? false : true;
    }

    @Override
    public int queryProjectRole(UserInfoVo userInfoVo) {
        String sql = "select case count(user_role) when 0 then 0 else user_role end from user_role where project_id = ? and user_id = ?";
        return queryForObject(sql,Integer.class,userInfoVo.getProjectId(),userInfoVo.getUserId());
    }

    @Override
    public int addUserRole(UserInfoVo userInfoVo) {
        //1.新增角色时，会将其他项目的状态置为已过期状态
        String firstSql = "update user_role set relation_state = 0 where user_id = ?";
        update(firstSql,userInfoVo.getUserId());
        //2.新增当前项目角色
        Long roleId = commonDao.getNextId("user_role","role_id");
        String sql = "insert into user_role(role_id,user_id,user_name,project_id,user_role,start_date,relation_state,opr_user_id,opr_user_name,opr_date) values(?,?,?,?,?,?,?,?,?,?)";
        return update(sql,roleId,userInfoVo.getUserId(),userInfoVo.getUserName(),userInfoVo.getProjectId(),userInfoVo.getUserRole(),getNowDate(),1,userInfoVo.getOprUserId(),userInfoVo.getOprUserName(),getNowDate());
    }

    @Override
    public int deleteUser(UserInfo userInfo) {
        String sql = "delete from user_info where user_id = ?";
        String roleSql = "update user_role set relation_state = 0 where user_id = ?";
        update(roleSql,userInfo.getUserId());
        return update(sql,userInfo.getUserId());
    }

    @Override
    public List<UserInfo> getProUserList(UserInfo userInfo) {
        String sql = "select distinct a.* from user_info a inner join user_role b on a.user_id = b.user_id where a.factory_id = ? and b.project_id = ?";
        return queryForList(sql,UserInfo.class,userInfo.getFactoryId(),userInfo.getProjectId());
    }

    @Override
    public int updatePassword(LoginUserVo loginUserVo) {
        String sql = "update login_user set login_password = ? where login_name = ?";
        return update(sql,loginUserVo.getLoginPassword(),loginUserVo.getLoginName());
    }

    @Override
    public List<Map<String,Object>> queryProRoleUserId(Long projectId, int userRole) {
        String sql = "select a.user_id as userId,a.jg_push_mark as jgPushMark,a.hw_push_mark as hwPushMark,a.ios_push_mark as iosPushMark from user_info a inner join user_role b on a.user_id = b.user_id where b.project_id = ? and b.user_role = ?";
        return queryForList(sql,projectId,userRole);
    }

    @Override
    public List<Map<String, Object>> queryUserList(String userIds) {
        String sql = "select a.user_id as userId,a.jg_push_mark as jgPushMark,a.hw_push_mark as hwPushMark,a.ios_push_mark as iosPushMark from user_info a where a.user_id in (" + userIds + ")";
        return queryForList(sql);
    }

    @Override
    public Long queryUserProjectId(Long userId) {
        String sql = "select project_id from user_role where user_id = ? and relation_state = 1";
        return queryForObject(sql,Long.class,userId);
    }

    @Override
    public List<UserInfo> queryMaterialAuditUserList(ProjectPo projectPo) {
        String sql = "select * from user_role where ((project_id = ? and user_role <> 1) or user_role = 9) and user_id <> ? and relation_state = 1";
        return queryForList(sql,UserInfo.class,projectPo.getProjectId(),projectPo.getOprUserId());
    }

    @Override
    public List<UserInfo> queryScheduleAuditUserList(ProjectPo projectPo) {
        String sql = "select * from user_role where ((project_id = ? and user_role = 8) or user_role = 9) and user_id <> ? and relation_state = 1";
        return queryForList(sql,UserInfo.class,projectPo.getProjectId(),projectPo.getOprUserId());
    }

    @Override
    public List<UserInfo> queryTechnologyManagerList(ProjectPo projectPo) {
        String sql = "select * from user_role where (project_id = ? and user_role = 1) and user_id <> ? and relation_state = 1";
        return queryForList(sql,UserInfo.class,projectPo.getProjectId(),projectPo.getOprUserId());
    }

    @Override
    public List<Map<String, Object>> queryUserStatistics(ProjectPo projectPo) {
        String sql = "select a.user_id as userid,a.user_name as username,b.inspect_id as inspectid,b.`status` as status,b.inspect_end_state as endstatus from user_role a left join factory_inspect b on a.project_id = b.project_id and a.user_id = b.person_liable_id   where a.user_role = 1 and a.project_id = ? order by a.user_id asc";
        return queryForList(sql,projectPo.getProjectId());
    }
}
